package com.example.bbs.dao.impl;


import com.example.bbs.dao.BaseDao;
import com.example.bbs.dao.PostDao;
import com.example.bbs.model.Post;
import com.example.bbs.utils.TimeUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
public class PostDaoImpl extends BaseDao implements PostDao {

    @Override
    public List<Post> getPostByBlockId(int blockId) {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Post> posts = new ArrayList<>();
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from post where block_id = ?");
            //填坑
            statement.setInt(1, blockId);
            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                posts.add(new Post(resultSet.getInt(1), resultSet.getString(2),
                        resultSet.getInt(4), TimeUtil.getStringToDate(resultSet.getString(7)),
                        resultSet.getInt(6), resultSet.getInt(5),
                        resultSet.getString(3)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return posts;
    }

    @Override
    public boolean addPost(Post post) {

        String sql = "insert into post values (default,?,?,?,?,?,?)";

        return executeUpdate(sql,post.getPostTitle(),post.getPostContent(),post.getUserId(),post.getBlockId(),post.getReplyNumber(),post.getPostTime());
    }

    @Override
    public boolean updateReplyNumber(int postId, int replyNumber) {

        String sql = "update post set reply_number = ? where post_id = ?";
        return executeUpdate(sql,replyNumber,postId);
    }

    @Override
    public List<Post> getPostByUserId(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Post> posts = new ArrayList<>();
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from post where user_id = ?");
            //填坑
            statement.setInt(1, userId);
            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                posts.add(new Post(resultSet.getInt(1), resultSet.getString(2),
                        resultSet.getInt(4), TimeUtil.getStringToDate(resultSet.getString(7)),
                        resultSet.getInt(6), resultSet.getInt(5),
                        resultSet.getString(3)));

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return posts;
    }

    @Override
    public boolean deletePostByPostId(int postId) {

        String sql = "delete from post where post_id = ?";
        return executeUpdate(sql,postId);
    }

    @Override
    public boolean updatePost(Post post) {

        String sql = "update post set post_content = ? where post_id = ?";
        return executeUpdate(sql,post.getPostContent(),post.getPostId());
    }

    @Override
    public Post findPostById(int postId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from post where post_id = ?");
            //填坑
            statement.setInt(1, postId);
            //执行
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
               return new Post(resultSet.getInt(1), resultSet.getString(2),
                        resultSet.getInt(4), TimeUtil.getStringToDate(resultSet.getString(7)),
                        resultSet.getInt(6), resultSet.getInt(5),
                        resultSet.getString(3));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return null;
    }


}
